package com.ruoyi.common.rabbitmq.utils;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.domain.SysUserNoticeInfo;
import com.ruoyi.common.rabbitmq.domain.EmailNotifyTemplate;
import com.ruoyi.common.rabbitmq.domain.SmsNotifyTemplate;
import com.ruoyi.common.rabbitmq.domain.WxNotifyTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author YangJun
 * @Date 2021年12月21日 11:09:00
 **/
public class UserNotifyTemplateInfoUtil {
    /**
     * 把通知的用户信息放到对应模板中
     *
     * @param userInfos         用户基础信息
     * @param emailTemplate     邮件模板,为空时不处理
     * @param smsNotifyTemplate 手机短信模板,为空时不处理
     * @param wxNotifyTemplate  微信通知模板,为空时不处理
     */
    public static void getNoticeSendUsers(List<SysUserNoticeInfo> userInfos, EmailNotifyTemplate emailTemplate, SmsNotifyTemplate smsNotifyTemplate, WxNotifyTemplate wxNotifyTemplate) {
        userInfos.forEach(userInfo -> {
            if (StringUtils.isNotEmpty(userInfo.getNotifyTypeIds())) {
                //用户有选择通知类型
                for (Integer notifyType : Convert.toIntArray(userInfo.getNotifyTypeIds())) {
                    switch (notifyType) {
                        case 1:
                            getPhoneUserInfos(userInfo, smsNotifyTemplate);
                            break;
                        case 2:
                            getEmailUserInfos(userInfo, emailTemplate);
                            break;
                        case 3:
                            getWxUserInfos(userInfo, wxNotifyTemplate);
                            break;
                    }
                }
            } else {
                //用户未选择通知类型
                getPhoneUserInfos(userInfo, smsNotifyTemplate);
                getEmailUserInfos(userInfo, emailTemplate);
                getWxUserInfos(userInfo, wxNotifyTemplate);
            }

        });
    }

    private static void getWxUserInfos(SysUserNoticeInfo userInfo, WxNotifyTemplate wxNotifyTemplate) {
        String openId =userInfo.getWxOpenId();
        if (StringUtils.isNotEmpty(openId) && wxNotifyTemplate != null && !wxNotifyTemplate.getToUsers().contains(openId)) {
            wxNotifyTemplate.getToUsers().add(openId);
            wxNotifyTemplate.getNotifyUserId().add(userInfo.getUserId());
            wxNotifyTemplate.getNotifyUserName().add(userInfo.getNickName());
        }

    }

    private static  void getEmailUserInfos(SysUserNoticeInfo userInfo, EmailNotifyTemplate emailTemplate) {
        String email=userInfo.getEmail();
        if (StringUtils.isNotEmpty(email) && emailTemplate != null && !emailTemplate.getTargetEmails().contains(email)) {
            emailTemplate.setTargetEmails(emailTemplate.getTargetEmails() + "," + email);
            emailTemplate.getNotifyUserId().add(userInfo.getUserId());
            emailTemplate.getNotifyUserName().add(userInfo.getNickName());
        }
    }

    private static  void getPhoneUserInfos(SysUserNoticeInfo userInfo, SmsNotifyTemplate smsNotifyTemplate) {
        String phonenumber = userInfo.getPhonenumber();
        if (StringUtils.isNotEmpty(phonenumber) && smsNotifyTemplate != null && !smsNotifyTemplate.getPhoneNumberJson().contains(phonenumber)) {
            List<String> phones = new ArrayList<>();
            try {
                phones.addAll(JSONArray.parseArray(smsNotifyTemplate.getPhoneNumberJson(), String.class));
            } catch (Exception e) {
                phones.add(phonenumber);
            }
            String phoneNumberJson = JSONArray.toJSONString(phones);
            smsNotifyTemplate.setPhoneNumberJson(phoneNumberJson);
            smsNotifyTemplate.getNotifyUserId().add(userInfo.getUserId());
            smsNotifyTemplate.getNotifyUserName().add(userInfo.getNickName());
        }
    }
}
